package org.qiyu.live.im.core.server.handler.Impl;

import com.alibaba.fastjson.JSON;
import io.netty.channel.ChannelHandlerContext;
import jakarta.annotation.Resource;
import org.qiyu.live.im.core.server.Servicer.ImMsgAckCheckServicer;
import org.qiyu.live.im.core.server.common.ImContextUtils;
import org.qiyu.live.im.core.server.common.ImMsg;
import org.qiyu.live.im.core.server.handler.SimplyHandler;
import org.qiyu.live.im.dto.ImMsgBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 类功能描述： 消息确定
 * @author 张广焱
 * @date 2023-12-11  18:14:35
 */
@Component
public class AckImMsgHandler implements SimplyHandler {
    private static final Logger logger = LoggerFactory.getLogger(AckImMsgHandler.class);
    @Resource
    private ImMsgAckCheckServicer imMsgAckCheckServicer;

    @Override
    public void handler(ChannelHandlerContext ctx, ImMsg imMsg) {
        Integer appId = ImContextUtils.getAppId(ctx);
        Long userId = ImContextUtils.getUserId(ctx);
        if (appId == null || userId == null) {
            logger.error("attr error,imMsg is {}", imMsg);
            //有可能是错误的消息包导致，直接放弃连接
            throw new IllegalArgumentException("attr is error");
        }
        imMsgAckCheckServicer.doMsgAck(JSON.parseObject(imMsg.getBody(), ImMsgBody.class));
    }
}
